#ifndef EDITFORMWRAPPER_H
#define EDITFORMWRAPPER_H



#include "AbstractEditFormActions.h"



#include <ItemModel.h>
#include <FileHandler.h>



#include <QWidget>
#include <QFileDialog>
#include <QMessageBox>
#include <QTimer>
#include <QDebug>


// todo: content of this code could be moved to BaseEditForm.
template<typename EDITFORM>
class EditFormWrapper : public EDITFORM
{
public:

    EditFormWrapper(ItemRoot* root, QWidget* parent = 0)
        : EDITFORM(root, parent)
    {
    }


    bool save()
    {
        if (!root()) return true;

        // update all appropriate data from this form
        updateData();

        QString file_path = root()->filePath();
        if (file_path.isEmpty() || !QFile::exists(file_path))
        {
            return saveAs();
        }

        return FileHandler::save(root(), file_path);
    }


    bool saveAs()
    {
        if (!root()) return true;

        QString file_path = QFileDialog::getSaveFileName(this, tr("Save File"), QDir::currentPath(), tr("Xml File (*.xml)"));
        if (file_path.isEmpty()) return true;

        return FileHandler::save(root(), file_path);
    }


    bool changed() const
    {
        if (!root()) return false;

        return root()->isDirty();
    }


};





#endif // EDITFORMWRAPPER_H


